package 爬楼梯;

import java.util.HashMap;

public class Soultion {
    public static void main(String[] args) {
        Soultion soultion = new Soultion();
        int i = soultion.climbStairs1(5);
        System.out.println(i);
    }
    //1.递归最简单
    public int climbStairs1(int n){
        if(n == 0) return 1;
        if(n == 1) return 1;
        return climbStairs1(n - 1) + climbStairs1(n - 2);
    }
    //2.递归优化，使用哈希表
    public int climbStairs2(int n) {
        HashMap<Integer,Integer> hashMap = new HashMap<Integer,Integer>();
        if(n==1) return 1;
        if(n==2) return 2;
        if(hashMap.containsKey(n)){
            return hashMap.get(n);
        }
        int ret = climbStairs2(n-1)+climbStairs2(n-2);
        hashMap.put(n,ret);
        return ret;
    }

    //3.迭代,斐波那契数列
    public int climbStair3(int n){
        if(n==1) return 1;
        if(n==2) return 2;
        int prepre = 1;
        int pre = 2;
        int ret = 0;
        for(int i = 3; i <= n ; i++){
            ret = prepre + pre;
            prepre = pre;
            pre = ret;
        }
        return ret;
    }
}
